<?php
namespace app\admin\model;

use think\facade\Session;

class BalanceType extends \think\Model
{


    /**
     * 获取单条
     * @param $id
     * @return array|null|\PDOStatement|string|\think\Model
     */
    public function _findOne($id){
        return $this->where([['id', '=', $id]])->find();
    }
    
    
    /**
    * 获取所有的类型
    */
    public function _getAll(){
        return $this->select()->toArray();
    }
    
    /**
     * 列表
     * @param array $condition
     * @param int $page_size
     * @param string $order_by
     * @return \think\Paginator
     */
    public function _list($condition = [], $page_size = PAGE_SIZE_ADMIN, $order_by = 'created desc'){
        $obj = $this->alias('b')
            ->field("b.*, m.user_name, bt.title as type_name")
            ->join("member m", "m.id = b.mid", "LEFT")
            ->join("balance_type bt", "bt.id = b.type_id", "LEFT")
            ->order($order_by);
        $result = get_page($obj, $condition, $page_size);
       
        return $result;
    }

    /**
     * 删除
     * @param $id
     * @return bool
     */
    public function _remove($id){
        $result = $this->_findOne($id);
        if(!$result){
            $this->error = '无此数据';
            return false;
        }

        if(!$result->delete()){
            $this->error = '操作失败';
            return false;
        }
        
        return true;
    }
    /**
     * 批量删除
     * @param $id
     * @return bool
     */
    public function _removeAll($id){
        $ret = $this->where([['id', 'in', $id]])->delete();
        if(!$ret){
            $this->error = '操作失败';
            return false;
        }
        
        return true;
    }

    
    /**
    * 数据总条数
    */
    public function _count(&$obj, $where = []){
        return $obj->where($where)->count();
    }

    /**
    * 获取条件
    */
    public function _where($request){
        $p = isset($request['page']) ? $request['page'] : 1;
        $start = isset($request['start']) ? $request['start'] : '';
        $end = isset($request['end']) ? $request['end'] : '';
        $type = isset($request['type']) ? $request['type'] : '';
        $title = isset($request['title']) ? $request['title'] : '';
        
        $request['page'] = $p;
        $request['start'] = $start;
        $request['end'] = $end;
        $request['title'] = $title;
        $request['type'] = $type;
        
        $search = $request;
        
        $title = htmlspecialchars(addslashes(str_replace(' ', '', $request['title'])));
        $request['title'] = str_replace('%', "\%", $title);
        
        $tmp = $request;
        foreach($tmp as $k=>$v){
            if(empty($v)){
                unset($tmp[$k]);
            }
        }
        
        $where = '';
        foreach($tmp as $k=>$v){
            if($k == 'start'){
                $start = strtotime($v);
                $where .= "b.created >= $start and ";
            }elseif($k == 'end'){
                $end = strtotime($v) + 24*3600;
                $where .= "b.created <= $end and ";
            }elseif($k == 'title'){
                $where .= "m.user_name like '%$v%' and ";
            }elseif($k == 'type'){
                $where .= "b.type = $v and ";
            }
        }
        if(!empty($where)){
            $where = substr($where, 0, -4);
        }
        return array('search'=>$search,'where'=>$where);
    }
}